package cn.edu.thu.tsquality.adaptor.single;

import cn.edu.thu.tsquality.core.common.algorithm.IMultiputAlgorithm;
import cn.edu.thu.tsquality.core.common.datasource.CsvDataSource;
import cn.edu.thu.tsquality.core.common.datasource.IDataSource;
import cn.edu.thu.tsquality.core.common.table.Table;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyihan  on 2018/5/9 下午5:57.
 * E-mail address is yihanwang22@163.com.
 * Copyright © 2017 wangyihan. All Rights Reserved.
 *
 * @author wangyihan
 */
public class SingleMultiputAdaptor {
  private IMultiputAlgorithm algorithm;

  public void setAlgorithm(IMultiputAlgorithm algorithm) {
    this.algorithm = algorithm;
  }

  public void run(String[] inputFiles, String[] outputFiles) throws IOException {
    List<Table> tables = new ArrayList<>();
    for (String inputFilePath : inputFiles) {
      IDataSource dataSource = new CsvDataSource(inputFilePath);
      tables.add(dataSource.read());
    }
    List<Table> resultTables = this.algorithm.run(tables);
    for (int i = 0; i < outputFiles.length && i < resultTables.size(); i++) {
      IDataSource resultSource = new CsvDataSource(outputFiles[i]);
      resultSource.write(resultTables.get(i));
    }
  }
}
